package com.simen.sample.activitygroup.base;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;

import com.simen.sample.activitygroup.MainTab1Activity;
import com.simen.sample.activitygroup.MainTab3Activity;
import com.simen.sample.activitygroup.MainTab4Activity;
import com.simen.sample.activitygroup.R;

/**
 * @author Created by Simen.
 * @date 2018/6/18 10:22
 * @modify Simen
 */
public class MyActivityGroup extends ActivityGroup implements View.OnClickListener {
    
    FrameLayout mContent;
    Button tab1;
    Button tab2;
    Button tab3;
    Button tab4;
    
    LocalActivityManager localActivityManager = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_group_activity);
        
        localActivityManager = getLocalActivityManager();
        
        mContent = findViewById(R.id.content);
        tab1 = findViewById(R.id.tab1);
        tab2 = findViewById(R.id.tab2);
        tab3 = findViewById(R.id.tab3);
        tab4 = findViewById(R.id.tab4);
        
        showTab1();
        
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);
        
    }
    
    private void showTab1() {
        showTab(MainTab1Activity.class);
    }
    
    private void showTab3() {
        showTab(MainTab3Activity.class);
    }
    
    private void showTab4() {
        showTab(MainTab4Activity.class);
    }
    
    private void showTab(Class<?> classAct) {
        Intent childIntent = new Intent(this, classAct);
        
        Window childWindow = localActivityManager.startActivity(classAct.getCanonicalName(), childIntent);
        View childView = childWindow.getDecorView();
        
        if (mContent.indexOfChild(childView) != -1) {
            mContent.bringChildToFront(childView);
            return;
        }
        
        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        childView.setLayoutParams(param);
        
        mContent.addView(childView);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab1:
                showTab1();
                break;
            case R.id.tab2:
                break;
            case R.id.tab3:
                showTab3();
                break;
            case R.id.tab4:
                showTab4();
                break;
        }
    }
}
